package com.stx.jeepay.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stx.jeepay.core.dao.SysUserAuthDao;
import com.stx.jeepay.core.entity.SysUserAuthEntity;
import com.stx.jeepay.core.enums.CS;
import com.stx.jeepay.core.service.SysUserAuthService;
import com.stx.jeepay.core.utils.StringKit;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("sysUserAuthService")
public class SysUserAuthServiceImpl extends ServiceImpl<SysUserAuthDao, SysUserAuthEntity> implements SysUserAuthService {

    /**
     * 根据登录信息查询用户认证信息
     */
    @Override
    public SysUserAuthEntity selectByLogin(String identifier, Byte identityType, String sysType) {
        return baseMapper.selectByLogin(identifier, identityType, sysType);
    }

    /**
     * 添加用户认证表
     */
    @Override
    public void addUserAuthDefault(Long userId, String loginUserName, String telPhone, String pwdRaw, String sysType) {
        String salt = StringKit.getUUID(6); //6位随机数
        String userPwd = new BCryptPasswordEncoder().encode(pwdRaw);

        /** 用户名登录方式 */
        SysUserAuthEntity record = new SysUserAuthEntity();
        record.setUserId(userId);
        record.setCredential(userPwd);
        record.setSalt(salt);
        record.setSysType(sysType);
        record.setIdentityType(CS.AUTH_TYPE.LOGIN_USER_NAME);
        record.setIdentifier(loginUserName);
        save(record);

        /** 手机号登录方式 */
        record = new SysUserAuthEntity();
        record.setUserId(userId);
        record.setCredential(userPwd);
        record.setSalt(salt);
        record.setSysType(sysType);
        record.setIdentityType(CS.AUTH_TYPE.TELPHONE);
        record.setIdentifier(telPhone);
        save(record);
    }

    /**
     * 重置密码
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void resetAuthInfo(Long resetUserId, String authLoginUserName, String telphone, String newPwd, String sysType) {
        if (StringUtils.isNotEmpty(newPwd)){
            // 根据当前用户ID 查询出用户的所有认证记录
            List<SysUserAuthEntity> authList = list(new LambdaQueryWrapper<SysUserAuthEntity>()
                    .eq(SysUserAuthEntity::getSysType, sysType)
                    .eq(SysUserAuthEntity::getUserId, resetUserId));
            for (SysUserAuthEntity auth : authList) {
                if(StringUtils.isNotEmpty(auth.getSalt())){
                    //可能为其他登录方式， 不存在salt
                    continue;
                }
                SysUserAuthEntity updateRecord = new SysUserAuthEntity();
                updateRecord.setAuthId(auth.getAuthId());
                updateRecord.setCredential(new BCryptPasswordEncoder().encode(newPwd));
                updateById(updateRecord);
            }
        }
    }
}